package com.ys.openapi.organization.department;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.utils.HttpUtils;
import com.erp.utils.PropertiesUtils;
import com.erp.utils.WebApiUtils;
import com.ys.openapi.organization.entity.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色
 */
public class Role {
    //新增角色
    public Object add_role(RoleVo vo,String access_token ){
        //查询角色是否存在
        Map<String,Object>map=    get_role_by_name(vo,access_token);
        if(map.get(vo.getRoleName()) !=null){
            return 0;
        }else {
              map=get_rolegroup_list(access_token);
              vo.setRoleGroupId(map.get("默认").toString());
        }

        //
        String url= PropertiesUtils.loadProperties("openapi").getProperty("add_role");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        String post = HttpUtils.doPost2(path,  JSON.toJSONString(vo),access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    //更新角色
    public Object modify_role(RoleVo vo,String access_token ){
        String oldname=vo.getRoleId();
        String newName=vo.getRoleName();
        String groupName=vo.getRoleGroupName();
        vo.setRoleName(oldname);
        vo.setRoleGroupName("");
        //查询角色是否存在
        Map<String,Object>map=    get_role_by_name(vo,access_token);
        if(map.get(vo.getRoleName()) ==null){
            return 0;
        }else{
            //取所属角色组信息
            JSONArray jbs=  (JSONArray)map.get("roles") ;
            JSONObject jb =(JSONObject) jbs.get(0);

            vo.setRoleId(map.get(vo.getRoleName()).toString());
            vo.setRoleName(newName);
           /* vo.setRoleGroupName(jb.getString("roleGroupName"));
            vo.setRoleGroupId(jb.getString("roleGroupId"));
            */

            //获取角色组id
            vo.setRoleGroupName(groupName);
            Map<String, Object> rolegroup_map = get_rolegroup_list(access_token);
            Object groupId=rolegroup_map.get(vo.getRoleGroupName());
            if(groupId ==null){
                vo.setRoleGroupId(rolegroup_map.get("默认").toString());
                vo.setRoleGroupName("默认");
            }else{
                vo.setRoleGroupId(groupId.toString());
                vo.setRoleGroupName(vo.getRoleGroupName());
            }


        }

        String url= PropertiesUtils.loadProperties("openapi").getProperty("modify_role");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        String post = HttpUtils.doPut2(path,  JSON.toJSONString(vo),access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    //删除角色
    public Object delete_role(List<String> vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("delete_role");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        //根据角色名称获取角色id
        List<String> li=new ArrayList<>();
        for(int i=0;i<vo.size();i++){
            RoleVo v=new RoleVo();
            v.setRoleName(vo.get(i));
            Map<String,Object>map=    get_role_by_name(v,access_token);
            if(map.get(v.getRoleName()) ==null){
               continue;
            }else {
               li.add( map.get(v.getRoleName()).toString());
            }
            String path="http://"+ip+""+url+"?ids="+map.get(v.getRoleName()).toString();
            String post = HttpUtils.doGet2(path,access_token,null);
            Object parse = JSON.parse(post);
        }

        return 0;
    }
    //根据角色名称模糊查询角色组
    public Map<String,Object> get_role_by_name(RoleVo vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_role_by_name");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?roleName="+vo.getRoleName();
        String post = HttpUtils.doGet2(path,access_token,null);
        // System.out.println(JSON.toJSONString(vo));
        Object parse = JSON.parse(post);
         JSONArray datas = (JSONArray)((JSONObject) parse).get("data");
        Map<String,Object>map=new HashMap<>();
        if(datas == null){
            return map;
        }
        for(int i=0;i< datas.size();i++){
            JSONObject jb=datas.getJSONObject(i);
            map.put(jb.getString("roleName"),jb.getString("roleId"));
        }
        map.put("roles",datas);
        return map;
    }
    //根据角色id查询角色
    public Object get_role_byid(RoleVo vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_role_byid");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?roleId="+vo.getRoleId();
        String post = HttpUtils.doGet2(path,access_token,null);
        // System.out.println(JSON.toJSONString(vo));
        Object parse = JSON.parse(post);
        return parse;
    }
    // 根据角色编码查询角色
    public Object get_rolegroup_by_code(RoleVo vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_rolegroup_by_code");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?roleCode="+vo.getCode();
        String post = HttpUtils.doGet2(path,access_token,null);
        // System.out.println(JSON.toJSONString(vo));
        Object parse = JSON.parse(post);
        return parse;
    }
    //根据角色 roleId 获取角色下的用户列表,传空表示获取第一级角色的用户
    public Object get_user_byRole(RequestUserByRole vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_user_byRole");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?"+vo.toString2();
        String post = HttpUtils.doGet2(path,access_token,null);
        // System.out.println(JSON.toJSONString(vo));
        Object parse = JSON.parse(post);
        return parse;
    }
    //新增角色组
    public Object add_role_group(RoleGroupVo vo, String access_token ){
        //判断角色组是否存在
        Map<String,Object> map=this.get_rolegroup_list(access_token);
        if(map.get(vo.getRoleGroupName()) !=null){
            return 0;
        }

        //
        String url= PropertiesUtils.loadProperties("openapi").getProperty("add_role_group");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        String post = HttpUtils.doPost2(path,  JSON.toJSONString(vo),access_token);
        Object parse =   JSON.parse(post);
        return parse;
    }
    //更新角色组  测试不成功
    public Object modify_role_group(RoleGroupVo vo,String oldName, String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("modify_role_group");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        //获取角色组id
        Map<String, Object> rolegroup_list = this.get_rolegroup_list(access_token);
        vo.setRoleGroupId(rolegroup_list.get(oldName).toString());
        String post = HttpUtils.doPut2(path,  JSON.toJSONString(vo),access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    //删除角色组
    public Object delete_role_group(List<String> vo, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("delete_role_group");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?ids="+vo.toString();
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    // 获取角色组列表，默认展开第一级
    public Map<String,Object> get_rolegroup_list( String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_rolegroup_list");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url ;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        JSONArray datas = (JSONArray)((JSONObject) parse).get("data");
        Map<String,Object>map=new HashMap<>();
        for(int i=0;i< datas.size();i++){
            JSONObject jb=datas.getJSONObject(i);
            map.put(jb.getString("name"),jb.getString("id"));
        }
        map.put("group",datas);
        return map;
    }
    //根据角色组编码查询角色组详情
    public Object get_roleGroup_info(String roleGroupCode, String access_token){
        String url=PropertiesUtils.loadProperties("openapi").getProperty("get_roleGroup_info");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url+"?code="+roleGroupCode ;
        String post = HttpUtils.doGet2(path,access_token,null);
        Object parse = JSON.parse(post);
        return parse;
    }
    //新增角色关联 ok
    public Object add_role_user(RequestRoleAndUser vo, String access_token){
        String url= PropertiesUtils.loadProperties("openapi").getProperty("add_role_user");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        String post = HttpUtils.doPost2(path,  JSON.toJSONString(vo),access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    // 删除角色员工
    public Object del_role_user(RequestDeleteRoleAndUser vo, String access_token){
      /*  {
            "roleId":"2c2c80867ab40ee9017adc64538d0ba7",
                "deptIds":[],
            "userIds":["2c2c80867ab40ee9017ad92efb930783"],
            "ids":""
        }*/
        String url= PropertiesUtils.loadProperties("openapi").getProperty("del_role_user");
        String ip=PropertiesUtils.loadProperties("openapi").getProperty("ip");
        String path="http://"+ip+""+url;
        String post = HttpUtils.doPost2(path,  JSON.toJSONString(vo),access_token);
        Object parse = JSON.parse(post);
        return parse;
    }
    public static void main(String[] args) {
        WebApiUtils wau=new WebApiUtils();
        Map<String, String> token = wau.get_token("admin", "123456");
        System.out.println(token.get("access_token"));
        Role role=new Role();
        //删除角色员工
        List rdralist=new ArrayList();
        rdralist.add("2c2c80867ab40ee9017b0232f91d2549");
        RequestDeleteRoleAndUser rdra=new RequestDeleteRoleAndUser();
        rdra.setRoleId("2c2c80867a1f88d8017a8e0c768d234f");
        rdra.setUserIds(rdralist);
       // System.out.println(role.del_role_user(rdra,wau.getTokenString()));


        RoleVo rolevo=new RoleVo();
       /* {  "code": "test001",  "corpId": "",   "roleGroupId": "2c2c80867ab40ee9017adc482a770b9d",    "roleGroupName": "测试2组",  "roleId": "",
                "roleName": "测试员角色1",  "roleType": "OTHER"
        }*/
       // rolevo.setCode("test003");
        rolevo.setRoleName("采购主管");
        rolevo.setRoleType("OTHER");
       // rolevo.setRoleGroupId("");
       // rolevo.setRoleGroupName("");
       // System.out.println(role.get_role_by_name(rolevo,token.get("access_token")));

        RoleGroupVo  groupVo=new RoleGroupVo();
       /* { "childrens": [],
            "code": "group2","corpId": "","parentId": "","roleGroupId": "","roleGroupName": "测试2组"}*/
        groupVo.setCode("group6545");
        groupVo.setRoleGroupName("测试6545组");
       // System.out.println( role.add_role_group(groupVo,token.get("access_token")));
       //   System.out.println(role.get_roleGroup_info("group6545",wau.getTokenString()));
        rolevo.setRoleGroupName("测试4组");
        rolevo.setRoleGroupId("2c2c80867ab40ee9017adc6211440ba5");
       // System.out.println(role.add_role(rolevo,token.get("access_token")));
       //System.out.println(role.get_role_by_name(rolevo,token.get("access_token")));
      // System.out.println(role.modify_role(rolevo,token.get("access_token")));
        //添加角色与用户关联
        RequestRoleAndUser rrau=new RequestRoleAndUser();
        rrau.setRoleId("2c2c80867a1f88d8017a8e0c768d234f");
        rrau.setOuScope(new ArrayList());
        UserIds userIds=new UserIds();
        userIds.setId("2c2c80867ab40ee9017b021b5fe4253a");
        userIds.setName("test05");
        userIds.setUnitType(3);
        List<UserIds> list=new ArrayList<>();
        list.add(userIds);
        rrau.setUserIds(list);
       // System.out.println(role.add_role_user(rrau,wau.getTokenString()));
       //获取角色组列表，默认展开第一级
       //System.out.println(role.get_rolegroup_list(wau.getTokenString()));
        //更新角色组
        RoleGroupVo vo=new RoleGroupVo();
        vo.setRoleGroupName("测试222组");
       // System.out.println(role.modify_role_group(vo,"测试2组",wau.getTokenString()));
        //删除角色
        List<String> li=new ArrayList<>();
        li.add("测试角色003");
       // role.delete_role(li,wau.getTokenString());
    }













}
